<?php

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');
require(dirname(__FILE__) . '/includes/xajax.common.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

/* 初始化分页信息 */
$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;

$cate_price = -1;
$cate_size = intval($_POST['cate_size']);
$cate_color = intval($_POST['cate_color']);

if(isset($_POST['cate_price'])){
	$cate_price = intval($_POST['cate_price']);
}

if($_REQUEST['size']<>""){
$size =$_REQUEST['size'];
}else{
$size = isset($_CFG['page_size'])  && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 16;
}
$brand = isset($_REQUEST['brand']) && intval($_REQUEST['brand']) > 0 ? intval($_REQUEST['brand']) : 0;
$price_max = isset($_REQUEST['price_max']) && intval($_REQUEST['price_max']) > 0 ? intval($_REQUEST['price_max']) : 0;
$price_min = isset($_REQUEST['price_min']) && intval($_REQUEST['price_min']) > 0 ? intval($_REQUEST['price_min']) : 0;
$filter_attr = empty($_REQUEST['filter_attr']) ? '' : trim($_REQUEST['filter_attr']);

/* 排序、显示方式以及类型 */
$default_display_type = $_CFG['show_order_type'] == '0' ? 'list' : ($_CFG['show_order_type'] == '1' ? 'grid' : 'text');
$default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'DESC' : 'ASC';
$default_sort_order_type   = $_CFG['sort_order_type'] == '0' ? 'goods_id' : ($_CFG['sort_order_type'] == '1' ? 'shop_price' : 'last_update');

$sort  = (isset($_REQUEST['sort'])  && in_array(trim(strtolower($_REQUEST['sort'])), array('goods_id','click_count','shop_price', 'last_update'))) ? trim($_REQUEST['sort'])  : $default_sort_order_type;
$order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC')))                              ? trim($_REQUEST['order']) : $default_sort_order_method;
$display  = (isset($_REQUEST['display']) && in_array(trim(strtolower($_REQUEST['display'])), array('list', 'grid', 'text'))) ? trim($_REQUEST['display'])  : (isset($_COOKIE['OS']['display']) ? $_COOKIE['OS']['display'] : $default_display_type);

setcookie('OS[display]', $display, gmtime() + 86400 * 7);

    $children = get_children($cat_id);

    $cat = get_cat_info($cat_id);   // 获得分类的相关信息

$goodslist = category_get_goods($children, $brand, $price_min, $price_max, $ext, $size, $page, $sort, $order,$cate_color,$cate_size);
if($display == 'grid')
{
    if(count($goodslist) % 2 != 0)
    {
        $goodslist[] = array();
    }
}

$smarty->assign('goods_list',       $goodslist);

$smarty->assign('size_list', get_size_list());
$smarty->assign('color_list', get_color_list());
$smarty->assign('brand_list', get_brands());
$smarty->assign('xajax_handle', $xajax->getJavascript('js'));
$smarty->assign('imageservice',$_CFG['pre_fetch_img']);
$smarty->display('goods_list.html');

function get_size_list($cat_id=0){
	global $db, $os;
	$sql = "SELECT s.size_id,s.size_code,s.size_name FROM " . $os->table('size') . " s";
	if($cat_id > 0){
		$sql .= " LEFT JOIN " . $os->table('category_size') . " cs ON s.size_id=cs.size_id WHERE cs.cat_id=" . intval($cat_id);
	}
	return $db->getAll($sql);	
}

function get_color_list($cat_id=0){
	global $db, $os;
	$sql = "SELECT c.color_id,c.color_code,c.color_name FROM " . $os->table('color') . " c";
	if($cat_id > 0){
		$sql .= " LEFT JOIN " . $os->table('category_color') . " cc ON c.color_id=cc.color_id WHERE cc.color_id=" . intval($cat_id);
	}
	return $db->getAll($sql);		
}

/**
 * 获得分类下的商品
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods($children, $brand, $min, $max, $ext, $size, $page, $sort, $order, $cate_color, $cate_size)
{

    $display = $GLOBALS['display'];
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';

    if ($brand > 0)
    {
        $where .=  "AND g.brand_id=$brand ";
    }

    if ($min > 0)
    {
        $where .= " AND g.shop_price >= $min ";
    }

    if ($max > 0)
    {
        $where .= " AND g.shop_price <= $max ";
    }

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name, g.goods_name_style, g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, ' .
            "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
            'g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img ' .
            'FROM ';

    if($cate_color > 0){
        $sql .= $GLOBALS['os']->table('goods_color')." AS gc, " ; 
    }        
    
    if($cate_size > 0){
        $sql .= $GLOBALS['os']->table('goods_size')." AS gs, " ;  
    }            
           
            
    $sql .=  $GLOBALS['os']->table('goods') . ' AS g LEFT JOIN '.$GLOBALS['os']->table('member_price')." AS mp ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]'"; 
            
    if ($cate_color > 0)
    {
        $where .= " AND gc.goods_id = g.goods_id AND gc.color_id = '$cate_color' ";
    }

    if ($cate_size > 0)
    {
        $where .= " AND gs.goods_id = g.goods_id AND gs.size_id = '$cate_size' ";
    } 
            
    $sql .= " WHERE $where $ext ORDER BY $sort $order";

    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }

        /* 处理商品水印图片 */
        $watermark_img = '';

        if ($promote_price != 0)
        {
            $watermark_img = "watermark_promote_small";
        }
        elseif ($row['is_new'] != 0)
        {
            $watermark_img = "watermark_new_small";
        }
        elseif ($row['is_best'] != 0)
        {
            $watermark_img = "watermark_best_small";
        }
        elseif ($row['is_hot'] != 0)
        {
            $watermark_img = 'watermark_hot_small';
        }

        if ($watermark_img != '')
        {
            $arr[$row['goods_id']]['watermark_img'] =  $watermark_img;
        }

        $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
        if($display == 'grid')
        {
            $arr[$row['goods_id']]['goods_name']       = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
        }
        else
        {
            $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
        }
        $arr[$row['goods_id']]['name']             = $row['goods_name'];
        $arr[$row['goods_id']]['goods_brief']      = $row['goods_brief'];
        $arr[$row['goods_id']]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
        $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
        $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
        $arr[$row['goods_id']]['type']             = $row['goods_type'];
        $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';
        $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
        $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
        $arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
    }

    return $arr;
}

/**
 * 获得分类的信息
 *
 * @param   integer $cat_id
 *
 * @return  void
 */
function get_cat_info($cat_id)
{
    return $GLOBALS['db']->getRow('SELECT keywords, cat_desc, style, grade, filter_attr, parent_id FROM ' . $GLOBALS['os']->table('category') .
        " WHERE cat_id = '$cat_id'");
}
?>